package controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import workerwithdb.managers.ServerHandler;
import autoservice.api.controller.IAutoservice;
import autoservice.api.messages.Messager;

public class Autoservice implements IAutoservice {

	private ServerHandler serverHandler;
	private ArrayList<String> exceptionMap = new ArrayList<String>();
	private static IAutoservice AUTOSERVICE;
	private static final String EMPLOYMENT = "employment";
	private static final String FILLING_DATE = "fillingDate";
	private static final String DEADLINE_DATE = "deadlineDate";
	private static final String BEGINNING_DATE = "beginningDate";

	private Autoservice() {
		serverHandler = new ServerHandler();
	}

	public static IAutoservice getInstance() {
		if (AUTOSERVICE == null) {
			AUTOSERVICE = new Autoservice();
		}
		return AUTOSERVICE;
	}
	
	private Date getDateOperation(){
		Date date = new Date();
		return date;
	}
	public void setOperation(String userLogin, String nameOperation){
		serverHandler.setOperation(userLogin, nameOperation, getDateOperation());
	}
	
	
	
	public Boolean saveUser(String login,String password,String name){
		return serverHandler.saveUser(name, login, password);
	}
	
	public String getAutorization(String login,String password){
		return serverHandler.getAutorization(login, password);
	}

	public ArrayList<String> getEmptyGarage() {
		return serverHandler.getEmptyPlaces();
	}

	public ArrayList<String> SortMaster(String parameters) {
		if (parameters.equals(EMPLOYMENT)) {

			return serverHandler.sortMasterByEmployment();
		} else {
			return serverHandler.sortMasterByName();
		}
	}

	public ArrayList<String> SortAllOrder(String parameters) {
		if (parameters.equals(FILLING_DATE)) {
			return serverHandler.sortOrderByFillingDate();
		} else if (parameters.equals(BEGINNING_DATE)) {
			return serverHandler.sortOrderByBeginningDate();
		} else if (parameters.equals(DEADLINE_DATE)) {
			return serverHandler.sortOrderByDeadlineDate();
		} else {
			return serverHandler.sortOrderByPrice();
		}
	}

	public ArrayList<String> SortMadingOrder(String parameters) {
		if (parameters.equals(FILLING_DATE)) {
			return serverHandler.sortMadingOrderByFillinggDate();
		} else if (parameters.equals(BEGINNING_DATE)) {
			return serverHandler.sortMadingOrderByBeginningDate();
		} else if (parameters.equals(DEADLINE_DATE)) {
			return serverHandler.sortMadingOrderByDeadlineDate();
		} else {
			return serverHandler.sortMadingOrderByPrice();
		}
	}

	public ArrayList<String> SortDeletedOrderByPeriod(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(1);
			Date end = ClassParserObject.getDate(2);
			String date = ClassParserObject.getString(0);
			if (date.equals(FILLING_DATE)) {
				return serverHandler.sortDeletedOrderByPeriodByFillingDate(
						begin, end);
			} else if (parameters.equals(BEGINNING_DATE)) {
				return serverHandler.sortDeletedOrderByPeriodByBeginningDate(
						begin, end);
			} else if (parameters.equals(DEADLINE_DATE)) {
				return serverHandler.sortDeletedOrderByPeriodByDeadlineDate(
						begin, end);
			} else {
				return serverHandler
						.sortDeletedOrderByPeriodByPrice(begin, end);
			}

		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortCanceledOrderByPeriod(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(1);
			Date end = ClassParserObject.getDate(2);
			String date = ClassParserObject.getString(0);
			if (date.equals(FILLING_DATE)) {
				return serverHandler.sortCanceledOrderByPeriodByFillingDate(
						begin, end);
			} else if (parameters.equals(BEGINNING_DATE)) {
				return serverHandler.sortCanceledOrderByPeriodByBeginningDate(
						begin, end);
			} else if (parameters.equals(DEADLINE_DATE)) {
				return serverHandler.sortCanceledOrderByPeriodByDeadlineDate(
						begin, end);
			} else {
				return serverHandler.sortCanceledOrderByPeriodByPrice(begin,
						end);
			}
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> SortMadeOrderByPeriod(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			Date begin = ClassParserObject.getDate(1);
			Date end = ClassParserObject.getDate(2);
			String date = ClassParserObject.getString(0);
			if (date.equals(FILLING_DATE)) {
				return serverHandler.sortMadeOrderByPeriodByFillingDate(begin,
						end);
			} else if (parameters.equals(BEGINNING_DATE)) {
				return serverHandler.sortMadeOrderByPeriodByBeginningDate(
						begin, end);
			} else if (parameters.equals(DEADLINE_DATE)) {
				return serverHandler.sortMadeOrderByPeriodByDeadlineDate(begin,
						end);
			} else {
				return serverHandler.sortMadeOrderByPeriodByPrice(begin, end);
			}
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> getOrderByMaster(String parameters) {
		return serverHandler.getOrderByMaster(parameters);
	}

	public ArrayList<String> getMasterByOrder(String parameters) {
		Integer numberOrder = Integer.parseInt(parameters);
		return serverHandler.getMasterByOrder(numberOrder);
	}

	public ArrayList<String> getFreePlacesOnDate(String parameters) {
		try {
			ClassParserObject.setArray(parameters);
			return serverHandler.getEmptyPlacesOnDate(ClassParserObject
					.getDate(0));
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> getNextFreeDate() {
		return serverHandler.getNextFreeDate();
	}

	public ArrayList<String> addMaster(String parameters) {
		return serverHandler.addMaster(parameters);
	}

	public ArrayList<String> deleteMaster(String parameters) {
		return serverHandler.deleteMaster(parameters);
	}

	public ArrayList<String> addGarage(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.addGarage(ClassParserObject.getInt(0));
	}

	public ArrayList<String> deleteGarage(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.deleteGarage(ClassParserObject.getInt(0));
	}

	public ArrayList<String> addOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		Integer orderNumber = ClassParserObject.getInt(0);
		try {
			Date fillingDate = ClassParserObject.getDate(1);
			Date deadlineDate = ClassParserObject.getDate(2);
			Date beginingDate = ClassParserObject.getDate(3);
			Integer price = ClassParserObject.getInt(4);
			String masterName = ClassParserObject.getString(5);
			Integer numberGarage = ClassParserObject.getInt(6);

			return serverHandler
					.addOrder(orderNumber, fillingDate, deadlineDate,
							beginingDate, price, masterName, numberGarage);

		} catch (ParseException e) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;

		}
	}

	public ArrayList<String> markOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		if (ClassParserObject.getString(1).equals("DELETED")) {
			return serverHandler.markOrderDeleted(ClassParserObject.getInt(0));
		} else if (ClassParserObject.getString(1).equals("MADE")) {
			return serverHandler.markOrderMade(ClassParserObject.getInt(0));
		} else {
			return serverHandler.markOrderCanceled(ClassParserObject.getInt(0));
		}

	}

	public ArrayList<String> ShiftTimeOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		try {
			Date newDate = ClassParserObject.getDate(0);
			Integer numberOrder = ClassParserObject.getInt(1);
			return serverHandler.shiftOrdersDeadlineDate(newDate, numberOrder);
		} catch (ParseException pe) {
			exceptionMap.clear();
			exceptionMap.add(Messager.WRONG_DATE_FORMAT);
			return exceptionMap;
		}
	}

	public ArrayList<String> cloneOrder(String parameters) {
		ClassParserObject.setArray(parameters);
		return serverHandler.cloneOrder(ClassParserObject.getInt(0));
	}

	public ArrayList<String> exportGarages(String url) {
		return serverHandler.exportGarages(ClassParserObject.replaceUrl(url));

	}

	public ArrayList<String> exportMasters(String url) {
		return serverHandler.exportMasters(ClassParserObject.replaceUrl(url));

	}

	public ArrayList<String> exportOrders(String url) {
		return serverHandler.exportOrders(ClassParserObject.replaceUrl(url));
	}

	public ArrayList<String> importGarages(String url) {
		return serverHandler.importGarages(ClassParserObject.replaceUrl(url));
	}

	public ArrayList<String> importMasters(String url) {
		return serverHandler.importMasters(ClassParserObject.replaceUrl(url));
	}

	public ArrayList<String> importOrders(String orderPath, String masterPath,
			String garagePath) {
		return serverHandler.importOrders(
				ClassParserObject.replaceUrl(orderPath),
				ClassParserObject.replaceUrl(masterPath),
				ClassParserObject.replaceUrl(garagePath));
	}

}
